package tools

import (
	"fmt"
	"time"
)

// FormatTime 格式化时间
func FormatTime(t time.Time) string {
	return t.Format("2006-01-02 15:04:05")
}

//大约输出
//rule: 1d=24h 1m=31d  1y=365.13d
func FormatMonthDayhms(sub time.Duration) (str string) {
	y := float32(sub) / Year
	nmonth := sub % time.Duration(Year)
	mon := nmonth / Month
	nday := nmonth % Month
	d := nday / Day
	nHour := nday / Day
	h := nHour / time.Hour
	nMinute := nHour % time.Minute
	m := nMinute / time.Minute
	s := nMinute % time.Second
	str = fmt.Sprintf("%dy%dm%dd%dh%dm%ds", int64(y), mon, d, h, m, s)
	return
}

// UnFormatTime 将时间字符串转为Time
func UnFormatTime(str string) (time.Time, error) {
	return time.Parse("2006-01-02 15:04:05", str)
}

// UnixTime 将时间转化为毫秒数
func UnixTime(t time.Time) int64 {
	return t.UnixNano() / 1000000
}

// UnunixTime 将毫秒数转为为时间
func UnunixTime(unix int64) time.Time {
	return time.Unix(0, unix*1000000)
}
